package 并发编程12.同步屏障;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * @author idea
 * @data 2019/1/28
 */
public class CyclicBarrierDemo {


    public static void main(String[] args) {
        //同步屏障的初始化位置
        CyclicBarrier barrier=new CyclicBarrier(3, new Runnable() {
            @Override
            public void run() {
                //当屏障的数值变为-1之后执行
                System.out.println("this is barrier handle!!1");
            }
        });

        for(int i=0;i<3;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        barrier.await();
                        System.out.println("屏障开启！");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }


    }

}
